<template>
  <div class="aside">
    <ul class="piece">
      <li
        v-for="(item, index) of piece"
        :key="index"
        @click="onChange(item)"
      >
        <div class="item">
          <i class="iconfont" :class="item.icon" />
          <em>{{item.name}}</em>
        </div>
      </li>
    </ul>
    <div class="content" v-if="current.name">
      <!-- 侧边栏项目详情 -->
      <component :is="currentComponent"></component>
      <!-- 收起 -->
      <div class="close" @click="onClose">
        <i class="el-icon-arrow-left"></i>
      </div>
    </div>
  </div>
</template>

<script>
import { categoryConstants } from '../../config'
import {
  AsideSlide,
  AsideNavigation,
  AsideCoupon,
  AsideRecommend,
  AsideFunction
} from './components'
export default {
  name: 'Aside',
  components: {
    AsideSlide,
    AsideNavigation,
    AsideCoupon,
    AsideRecommend,
    AsideFunction
  },
  data () {
    return {
      current: {}, // 当前选中的侧边栏
      currentComponent: {}, // 当前选中的侧边栏组件
      piece: [ // 侧边栏目
        {
          icon: 'icon-lunbo',
          name: '轮播',
          value: categoryConstants.SLIDE
        },
        {
          icon: 'icon-daohang',
          name: '导航',
          value: categoryConstants.NAVIGATION
        },
        {
          icon:'icon-shangpin',
          name: '推荐',
          value: categoryConstants.RECOMMEND
        },
        {
          icon: 'icon-youhuiquan',
          name: '优惠券',
          value: categoryConstants.COUPON
        },
        {
          icon:'icon-qita2',
          name: '功能',
          value: categoryConstants.FUNCTION
        }
      ]
    }
  },
  methods: {
    onChange (row) {
      if (this.current && this.current.value === row.value) {
        return this.onClose()
      }
      this.current = row
      switch (row.value) {
        case categoryConstants.SLIDE:
          this.currentComponent = AsideSlide
          break
        case categoryConstants.NAVIGATION:
          this.currentComponent = AsideNavigation
          break
        case categoryConstants.COUPON:
          this.currentComponent = AsideCoupon
          break
        case categoryConstants.RECOMMEND:
          this.currentComponent = AsideRecommend
          break
        case categoryConstants.FUNCTION:
          this.currentComponent = AsideFunction
          break
        default:
          this.current = {}
          break
      }
    },
    onClose () {
      this.current = {}
      this.currentComponent = {}
    }
  }
}
</script>

<style lang="less" scoped>
.aside {
  background: #fff;
  min-width: 65px;
  position: relative;
  z-index: 90;
  box-shadow: 3px 0 4px rgba(0, 0, 0, .1);
  .piece {
    .item {
      text-align: center;
      padding: 10px 0;
      cursor: pointer;
      margin-bottom: 1px;
      &.active {
        color: #e23738;
        background: #fafafa;
      }
      &:hover {
        background: #fafafa;
      }
      i, em {
        display: block;
        font-size: normal;
      }
      i {
        font-size: 22px;
      }
      em {
        font-size: 12px;
        // padding-top: 6px;
      }
    }
  }
  .content {
    position: absolute;
    padding: 0 10px;
    left: 65px;
    top: 0;
    background: #fff;
    // width: 330px;
    width: 250px;
    height: 100%;
    box-shadow: 3px 3px 10px rgba(0, 0, 0, .1);
    z-index: 50;
    // 侧边栏展开后的拓展项
    & ::v-deep .expect {
      height: 100%;
      display: flex;
      align-items: center;
      text-align: center;
      color: #999;
      span {
        flex: 1;
      }
    }
    .close {
      position: absolute;
      left: 100%;
      top: 50%;
      width: 16px;
      height: 60px;
      border-radius: 0 5px 5px 0;
      display: flex;
      align-items: center;
      text-align: center;
      background: #fff;
      transform: translate3d(0, -50%, 0);
      box-shadow: 3px 3px 10px rgba(0, 0, 0, .1);
      cursor: pointer;
    }
    // 侧边栏展开通用模板样式
    & ::v-deep .aside-template {
      display: flex;
      flex-direction: column;
      height: 100%;
      .content {
        width: 100%;
        box-sizing: border-box;
        overflow: hidden;
        .item {
          text-align: center;
          padding: 10px 0;
          cursor: pointer;
          margin-bottom: 1px;
          width: 50%;
          float: left;
          i, em {
            display: block;
            font-size: normal;
          }
          i {
            font-size: 30px;
          }
          em {
            font-size: 12px;
          }
        }
        &:after {
          clear: both;
        }
      }
    }
    & ::v-deep .title {
      font-weight: bold;
      padding: 10px 0;
      line-height: 1;
    }
    & ::v-deep .list {
      overflow: auto;
      flex: 1;
    }
  }
}

</style>